package org.springblade.modules.platform.utils;

import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @作者 wanglang
 * @创建时间 19-6-28 下午5:37
 * @描述
 **/
@Component
public class TimeUtils {

    /**
     * 时间戳转时间格式(yyyy-MM-dd)
     * @param timeStamp
     * @param timeType
     * @return
     */
    public static String timeStampConversionTime(long timeStamp,String timeType){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timeType);
        String format = dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneId.systemDefault()));
        return format;
    }

    /**
     * 传入String类型格式化时间，转换成Long类型的时间戳
     * @param strTime
     * @param timeType
     * @return
     */
    public static long TimeOfStrToLong(String strTime,String timeType){
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern(timeType);
        LocalDateTime parse = LocalDateTime.parse(strTime, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

    }

    /**
     * 时间转化为String格式
     * @param localDateTime
     * @param timeType
     * @return
     */
    public static String getTimeStrNow1(LocalDateTime localDateTime,String timeType){
        String format = localDateTime.format(DateTimeFormatter.ofPattern(timeType));
        return format;
    }

    /**
     * 传入String类型格式化时间
     * @param time
     * @param timeType
     * @return
     */
    public static LocalDateTime getTimeStrNow2(String time,String timeType){
        LocalDateTime parse1 = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(timeType));
        return parse1;
    }

    /**
     * 将LocalDateTime转为long
     * @param localDateTime
     * @return
     */
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 将long类型转为LocalDateTime
     * @param timestamp
     * @return
     */
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * Date转换为LocalDateTime
     * @param date
     */
    public static LocalDateTime date2LocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }
    /**
     * LocalDateTime转换为Date
     * @param localDateTime
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * 当前时间增加几天
     * @param localDateTime
     * @param n
     * @return
     */
    public static LocalDateTime localDateTimeAddDay(LocalDateTime localDateTime,Integer n){
        LocalDateTime localDateTime1 = localDateTime.plusDays(n);
        return  localDateTime1;
    }

    /**
     * 当前时间减几天
     * @param localDateTime
     * @param n
     * @return
     */
    public static LocalDateTime localDateTimeSubtractionDay(LocalDateTime localDateTime,Integer n){
        LocalDateTime localDateTime1 = localDateTime.minusDays(n);
        return  localDateTime1;
    }

    /**
     *将日期转为字符串 yyyy-mm-dd HH:mm:ss
     * @param date
     * @param timeType
     * @return
     */
    public static String getYYYYMMDDHHMMSS(Date date,String timeType) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeType);
        LocalDateTime localDateTime = date2LocalDateTime(date);
        return formatter.format(localDateTime);
    }

    /**
     * 将字符串转为日期
     * @param date
     * @param timeType
     * @return
     */
    public static Date getDate(String date,String timeType) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timeType);
        LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
        return localDateTime2Date(localDateTime);
    }


    /**
     * 获取本月的第一天 00:00:00
     * @return
     */
    public static Date currentFirstDayOfMonth() {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return localDateTime2Date(localDateTime);
    }

    /**
     * 获取两个日期相差多少天
     * @param date1
     * @param date2
     * @return
     */
    public static Long getUntilDay(Date date1, Date date2) {
        LocalDate localDate1 = dateToLocalDate(date1);
        LocalDate localDate2 = dateToLocalDate(date2);
        return ChronoUnit.DAYS.between(localDate1, localDate2);
    }

    /**
     * date 转LocalDate
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate;
    }

    /**
     * localDate 转 date
     * @param localDate
     * @return
     */
    public static Date localToDate(LocalDate localDate) {
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        return date;
    }

    /**
     * 获取当前时间23：59：59
     * @param date
     * @return
     */
    public static Date currentMax(Date date) {
        LocalDateTime localDateTime = date2LocalDateTime(date);
        localDateTime = localDateTime.with(LocalTime.MAX);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 当前时间00:00:00
     * @param date
     * @return
     */
    public static Date currentMin(Date date) {
        LocalDateTime localDateTime = date2LocalDateTime(date);
        localDateTime = localDateTime.with(LocalTime.MIN);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
/*
	public static void main(String[] args) {
		Long L = Long.parseLong("1639623360507");
		long time = currentMin(new Date(L)).getTime();
		long time1 = currentMax(new Date(L)).getTime();
		System.out.println(time);
		System.out.println(time1);
	}*/

}

